package com.hawkfly.action;

import java.io.IOException;
import java.io.StringWriter;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.codehaus.jackson.JsonFactory;
import org.codehaus.jackson.JsonGenerator;
import org.codehaus.jackson.map.ObjectMapper;

import com.wbcs.config.component.IComponentConfigBean;
import com.wbcs.jbsf.dao.IDataModal;
import com.wbcs.jbsf.dao.JBSFDaoTemplate;
import com.wbcs.jbsf.util.Consts;
import com.wbcs.jbsf.util.JacksonMapper;
import com.wbcs.system.ReportRequest;
import com.wbcs.system.serveraction.IServerAction;

public class SelectCascadeAction4sdcm implements IServerAction
{
    public String executeSeverAction(ReportRequest rrequest,IComponentConfigBean ccbean,List<Map<String,String>> lstData,
            Map<String,String> mCustomizedData)
    {
        return null;
    }

    public String executeServerAction(HttpServletRequest request,HttpServletResponse response,List<Map<String,String>> lstData)
    {
     // 在lstData中按照siteid进行查询，并返回查询结果为json
        JBSFDaoTemplate jdaoTemplate = new JBSFDaoTemplate(false);
        StringWriter sw = new StringWriter();
        String towhere = lstData.get(0).get("towhere");
        try{
	        if(towhere == null)
	        {
	            StringBuffer sql = new StringBuffer("select id,realname from t_user_info where 1=0 or username in(select usercode from jbsf_r_rolesusers where ext1 = '1' and ext2 like ?)");
	            
	            Map<String, Object> rowObjs = 
	            jdaoTemplate.<Map<String, Object>>select(sql.toString(),new IDataModal<Map<String, Object>>()
	            {
	                public Map<String,Object> loadDatas(ResultSet rs,int ccount,ResultSetMetaData rmd,Map<String,Object> dataModal)
	                        throws SQLException
	                {
	                    dataModal = new LinkedHashMap<String,Object>();
	                    while(rs.next()){
	                        dataModal.put(String.valueOf(rs.getInt(1)),rs.getString(2));
	                    }
	                    return dataModal;
	                }
	            },"%"+lstData.get(0).get("sectionid")+"%");
	            ObjectMapper jsonMapper = JacksonMapper.getInstance();
	            JsonGenerator gen;
	            try
	            {
	                gen=new JsonFactory().createJsonGenerator(sw);
	                jsonMapper.writeValue(gen, rowObjs); 
	                gen.close();
	            }catch(IOException e)
	            {
	                e.printStackTrace();
	            }
	        }else{
	            Object siteid = request.getSession().getAttribute(Consts.SESSION_SITEID);
	            StringBuffer sql = new StringBuffer("select id,realname from t_user_info where 1=0 or username in(select usercode from jbsf_r_rolesusers where ext1 = ? and ext2 like ?)");
	            
	            Map<String, Object> rowObjs = 
	            jdaoTemplate.<Map<String, Object>>select(sql.toString(),new IDataModal<Map<String, Object>>()
	            {
	                public Map<String,Object> loadDatas(ResultSet rs,int ccount,ResultSetMetaData rmd,Map<String,Object> dataModal)
	                        throws SQLException
	                {
	                    dataModal = new LinkedHashMap<String,Object>();
	                    while(rs.next()){
	                        dataModal.put(String.valueOf(rs.getInt(1)),rs.getString(2));
	                    }
	                    return dataModal;
	                }
	            },siteid,"%"+lstData.get(0).get("sectionid")+"%");
	            ObjectMapper jsonMapper = JacksonMapper.getInstance();
	            JsonGenerator gen;
	            try
	            {
	                gen=new JsonFactory().createJsonGenerator(sw);
	                jsonMapper.writeValue(gen, rowObjs); 
	                gen.close();
	            }catch(IOException e)
	            {
	                e.printStackTrace();
	            }
	        }
        }catch(SQLException e){
        	e.printStackTrace();
        }
        return sw.toString();
    }

}

